using System;
using System.Collections.Generic;
using System.Text;

namespace cb0tServer
{
    class IdleMonitoring
    {
        private static List<IdleMonitoringItem> items = new List<IdleMonitoringItem>();

        public static void Clear()
        {
            items.Clear();
        }

        public static void SetUser(UserObject userobj)
        {
            foreach (IdleMonitoringItem i in items)
            {
                if (i.name == userobj.name)
                {
                    userobj.idle = true;
                    userobj.idle_time = i.time;
                    return;
                }
            }
        }

        public static String AddIdleUser(UserObject userobj)
        {
            userobj.idle = true;
            userobj.idle_time = cb0tServer.Helpers.UnixTimeMS();
            items.Add(new IdleMonitoringItem(userobj.name, userobj.idle_time));

            if (!userobj.user_message.StartsWith("[idle] "))
            {
                userobj.user_message = "[idle] " + userobj.user_message;
                UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.PersonalMessagePacket(userobj, userobj.user_message));
            }

            return StringTemplate.GetString(17).Replace("+n", userobj.name).Replace("+t", Now());
        }

        public static String RemIdleUser(UserObject userobj)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].name == userobj.name)
                {
                    items.RemoveAt(i);
                    userobj.idle = false;

                    if (userobj.user_message.StartsWith("[idle] "))
                    {
                        while (userobj.user_message.StartsWith("[idle] "))
                            userobj.user_message = userobj.user_message.Substring(7);

                        UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.PersonalMessagePacket(userobj, userobj.user_message.Length > 0 ? userobj.user_message : "  "));
                    }

                    return StringTemplate.GetString(18).Replace("+n", userobj.name).Replace("+t", Now()).Replace("+a", AwayTime(userobj.idle_time));
                }
            }

            return null;
        }

        private static String AwayTime(long n)
        {
            long s1 = cb0tServer.Helpers.UnixTimeMS() - n;

            int s = (int)Math.Round((double)s1 / 1000);

            if (s < 60) return s + (s == 1 ? " second" : " seconds");

            int m = (int)Math.Floor((double)s / 60);
            s -= (m * 60);

            if (m < 60) return m + (m == 1 ? " minute " : " minutes ") + s + (s == 1 ? " second" : " seconds");

            int h = (int)Math.Floor((double)m / 60);
            m -= (h * 60);

            return h + (h == 1 ? " hour " : " hours ") + m + (m == 1 ? " minute " : " minutes ") + s + (s == 1 ? " second" : " seconds");
        }

        private static String Now()
        {
            return DateTime.Now.ToShortTimeString();
        }
    }

    class IdleMonitoringItem
    {
        public String name;
        public long time;

        public IdleMonitoringItem(String name, long time)
        {
            this.name = name;
            this.time = time;
        }
    }

}
